/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.ejb;

import com.comut.crud.bo.UserHandler;
import com.comut.crud.connection.MooveFleetConnectionManager;
import com.comut.crud.ejb.interfaces.local.IUserActionLocal;
import com.comut.crud.ejb.interfaces.remote.IUserActionRemote;
import com.comut.crud.pojo.user.User;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.BaseException;
import com.comut.lib.exception.ObjectNotFoundException;
import java.sql.Connection;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
@Stateless
public class UserBean implements IUserActionLocal, IUserActionRemote {

    private static Logger _logger = Logger.getLogger(UserBean.class.getName());

    @PostConstruct
    private void userPostConstruct() {
        _logger.info("PostConstruct");
    }

    @PreDestroy
    private void userPreDestroy() {
        _logger.info("PreDestroy");
    }

    public UserBean() {
    }

    public User insertUser(User user) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        User toRet;
        UserHandler th;
        try {
            th = new UserHandler(connection);
            toRet = th.insertUser(user);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        return toRet;
    }

    public void deleteUser(User user) {
        if(user.getId()<1)
            throw new BaseException("The id must be a int bigger than 0", ErrorCodes.INVALID_ID);
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        try {
            UserHandler th = new UserHandler(connection);
            th.deleteUser(user);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
        }
    }

    public void updateUser(String user) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        try {
            UserHandler th = new UserHandler(connection);
            th.updateUser(user);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
        }
    }

    public User getUser(User user) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        User toRet = null;
        try {
            UserHandler th = new UserHandler(connection);
            toRet = th.getUser(user);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
        }
        return toRet;
    }

    /**
     * @author Paulo Fournier
     * Issue: IFL-1565
     * Been to check for the user's authentication
     * @param login The user's login
     * @param password The user's password
     * @return User if the authentication was successfully
     * @throws ObjectNotFoundException
     */
    public com.comut.crud.pojo.mobile.User checkAuthentication(String login, String password) throws ObjectNotFoundException {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        com.comut.crud.pojo.mobile.User ret =new com.comut.crud.pojo.mobile.User();
        try {
            UserHandler userHandler = new UserHandler(connection);
            ret = userHandler.checkAuthentication(login, password);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
        }
        return ret;
    }
}
